package com.zenithmind.note.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.note.pojo.dto.NoteCategoryDTO;
import com.zenithmind.note.pojo.query.NoteCategoryQuery;
import com.zenithmind.note.pojo.vo.NoteCategoryVO;
import com.zenithmind.note.service.NoteCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/zenithMind/note/category")
@RequiredArgsConstructor
@Tag(name = "笔记分类管理")
public class NoteCategoryController {
    
    private final NoteCategoryService categoryService;
    
    /**
     * 创建分类
     */
    @Log(description = "创建笔记分类", operationType = OperationTypeEnum.ADD)
    @PostMapping("/createCategory")
    @Operation(summary = "创建笔记分类")
    public Result<String> createCategory(@Validated @RequestBody NoteCategoryDTO categoryDTO) {
        log.info("创建笔记分类：{}", categoryDTO);
        return Result.success(categoryService.createCategory(categoryDTO));
    }
    
    /**
     * 更新分类
     */
    @Log(description = "更新笔记分类", operationType = OperationTypeEnum.UPDATE)
    @PutMapping("/updateCategory")
    @Operation(summary = "更新笔记分类")
    public Result<Void> updateCategory(@Validated @RequestBody NoteCategoryDTO categoryDTO) {
        log.info("更新笔记分类：{}", categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }
    
    /**
     * 删除分类
     */
    @Log(description = "删除笔记分类", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/deleteCategory/{id}")
    @Operation(summary = "删除笔记分类")
    public Result<Void> deleteCategory(@PathVariable String id) {
        log.info("删除笔记分类：{}", id);
        categoryService.deleteCategory(id);
        return Result.success();
    }
    
    /**
     * 获取分类详情
     */
    @Log(description = "获取笔记分类详情", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getCategoryDetail/{id}")
    @Operation(summary = "获取笔记分类详情")
    public Result<NoteCategoryVO> getCategoryDetail(@PathVariable String id) {
        log.info("获取笔记分类详情：{}", id);
        return Result.success(categoryService.getCategoryDetail(id));
    }
    
    /**
     * 分页查询分类列表
     */
    @Log(description = "分页查询笔记分类列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/categoryPage")
    @Operation(summary = "分页查询笔记分类列表")
    public Result<PageResult<NoteCategoryVO>> categoryPage(NoteCategoryQuery query) {
        log.info("分页查询笔记分类列表：{}", query);
        return Result.success(categoryService.categoryPage(query));
    }
    
    /**
     * 获取用户的分类列表
     */
    @Log(description = "获取用户的笔记分类列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getUserCategories")
    @Operation(summary = "获取用户的笔记分类列表")
    public Result<List<NoteCategoryVO>> getUserCategories(@RequestParam(required = false) String userId) {
        try {
            log.info("获取用户的笔记分类列表，userId：{}", userId);
            List<NoteCategoryVO> results = categoryService.getUserCategories(userId);
            return Result.success(results);
        } catch (Exception e) {
            log.error("获取用户的笔记分类列表异常", e);
            // 发生异常时返回空列表
            return Result.success(new ArrayList<>());
        }
    }
    
    /**
     * 获取系统预设分类列表
     */
    @Log(description = "获取系统预设笔记分类列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getSystemCategories")
    @Operation(summary = "获取系统预设笔记分类列表")
    public Result<List<NoteCategoryVO>> getSystemCategories() {
        log.info("获取系统预设笔记分类列表");
        return Result.success(categoryService.getSystemCategories());
    }
    
    /**
     * 获取分类统计信息
     */
    @Log(description = "获取笔记分类统计信息", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getCategoryStats")
    @Operation(summary = "获取笔记分类统计信息")
    public Result<List<NoteCategoryVO>> getCategoryStats(@RequestParam(required = false) String userId) {
        log.info("获取笔记分类统计信息，userId：{}", userId);
        return Result.success(categoryService.getCategoryStats(userId));
    }
} 